package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.recruitment_management_system.pojo.constant.SystemConstantEnum;
import com.team.recruitment_management_system.pojo.dto.chat.AddChatListDto;
import com.team.recruitment_management_system.pojo.dto.chat.ChangeChatListDto;
import com.team.recruitment_management_system.pojo.entity.ChatList;
import com.team.recruitment_management_system.pojo.vo.chat.ChatListVo;
import com.team.recruitment_management_system.service.ChatListService;
import com.team.recruitment_management_system.mapper.ChatListMapper;
import com.team.recruitment_management_system.util.JwtHelper;
import com.team.recruitment_management_system.util.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 86147
* @description 针对表【chat_list(聊天列表)】的数据库操作Service实现
* @createDate 2024-03-21 20:28:40
*/
@Service
public class ChatListServiceImpl extends ServiceImpl<ChatListMapper, ChatList>
    implements ChatListService{

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private ChatListMapper chatListMapper;

    @Override
    public Result<List<ChatListVo>> getMyCharListBySeeker(String token) {
        int userId = jwtHelper.getUserId(token);
        List<ChatListVo> chatListVos = chatListMapper.getMyCharListBySeeker(userId);
        return Result.ok(chatListVos);
    }

    @Override
    public Result<List<ChatListVo>> getMyCharListByHr(String token) {
        int userId = jwtHelper.getUserId(token);
        List<ChatListVo> chatListVos = chatListMapper.getMyCharListByHr(userId);
        return Result.ok(chatListVos);
    }

    @Override
    public Result<Void> addCharList(AddChatListDto addChatListDto) {
        LambdaQueryWrapper<ChatList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatList::getJobId,addChatListDto.getJobId())
                .eq(ChatList::getResumeId,addChatListDto.getResumeId());
        if(chatListMapper.exists(queryWrapper)){
            return Result.build(null,500,"该聊天对话已存在");
        }else{
            ChatList chatList = new ChatList();
            BeanUtils.copyProperties(addChatListDto,chatList);
            chatList.setCreateTime(new Date());
            this.save(chatList);
            return Result.putOk(null);
        }
    }

    @Override
    public Result<Void> changeChatHrList(ChangeChatListDto changeChatListDto) {
        LambdaQueryWrapper<ChatList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatList::getJobId,changeChatListDto.getJobId())
                .eq(ChatList::getResumeId,changeChatListDto.getResumeId())
                .eq(ChatList::getRecipientObject, SystemConstantEnum.ROLE_HR.getValue());
        ChatList chatList = this.getOne(queryWrapper);
        if(chatList == null){
            ChatList add = new ChatList();
            BeanUtils.copyProperties(changeChatListDto,add);
            add.setRecipientObject(SystemConstantEnum.ROLE_HR.getValue());
            add.setCreateTime(new Date());
            add.setLastTime(new Date());
            this.save(add);
        }else{
            BeanUtils.copyProperties(changeChatListDto,chatList);
            chatList.setLastTime(new Date());
            this.updateById(chatList);
        }
        return Result.putOk(null);
    }

    @Override
    public Result<Void> changeChatSeekerList(ChangeChatListDto changeChatListDto) {
        LambdaQueryWrapper<ChatList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatList::getJobId,changeChatListDto.getJobId())
                .eq(ChatList::getResumeId,changeChatListDto.getResumeId())
                .eq(ChatList::getRecipientObject, SystemConstantEnum.ROLE_SEEKER.getValue());
        ChatList chatList = this.getOne(queryWrapper);
        if(chatList == null){
            ChatList add = new ChatList();
            BeanUtils.copyProperties(changeChatListDto,add);
            add.setRecipientObject(SystemConstantEnum.ROLE_SEEKER.getValue());
            add.setCreateTime(new Date());
            add.setLastTime(new Date());
            this.save(add);
        }else{
            BeanUtils.copyProperties(changeChatListDto,chatList);
            chatList.setLastTime(new Date());
            this.updateById(chatList);
        }
        return Result.putOk(null);
    }

    @Override
    public Result<Void> deleteCharListById(int id) {
        ChatList chatList = this.getById(id);
        if(chatList == null){
            return Result.build(null,500,"不存在该聊天对话");
        }else{
            this.removeById(chatList);
            return Result.deleteOk(null);
        }
    }
}




